package com.chao.tlias.controller;

import com.chao.tlias.Pojo.Dept;
import com.chao.tlias.Pojo.Result;
import com.chao.tlias.anno.Log;
import com.chao.tlias.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门的控制器
 */

@Slf4j
@RestController()
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;

    /**
     * 查询所有部门信息
     *
     * @return
     */
    @GetMapping()
    public Result selectAll() {
        log.info("查询所有部门信息");
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }

    /**
     * 根据ID删除部门
     *
     * @param id
     * @return
     */
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除部门：{}",id);
        deptService.deleteById(id);
        return Result.success();
    }

    /**
     * 添加部门
     *
     * @param dept
     * @return
     */
    @Log
    @PostMapping()
    public Result add(@RequestBody Dept dept) {
        log.info("添加部门:{}", dept.getName());
        deptService.add(dept);
        return Result.success();
    }

    /**
     * 根据ID查询部门
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        log.info("根据id查询部门:{}", id);
        Dept dept = deptService.selectById(id);
        return Result.success(dept);
    }

    /**
     * 修改部门
     */
    @Log
    @PutMapping()
    public Result update(@RequestBody Dept dept) {
        log.info("修改部门:{}", dept);
        deptService.updateDept(dept);
        return Result.success();
    }

}
